/**
 * 部门路由模块
 */

const express = require('express');
const departmentController = require('../controllers/departmentController');
const { authenticateToken } = require('../middlewares/authMiddleware');

const { checkPermission , checkSelfOrPermission ,checkPermissionLevel} = require('../middlewares/permissionMiddleware');
const router = express.Router();

// 所有用户路由都需要认证Token
router.use(authenticateToken);

// 管理类接口（需要管理员权限）

// 获取所有部门
router.get('/', checkPermission('department:list'),checkPermissionLevel(20), departmentController.getAllDepartments);

// 获取部门树结构
router.get('/tree', checkPermission('department:view:tree'),checkPermissionLevel(20), departmentController.getDepartmentTree);

// 获取部门统计信息
router.get('/stats', checkPermission('department:view:stats'),checkPermissionLevel(20), departmentController.getDepartmentStats);

// 获取可选的部门负责人列表
router.get('/managers', checkPermission('department:view:managers'),checkPermissionLevel(20), departmentController.getManagerList);

// 获取部门详情
router.get('/:id', checkPermission('department:view'),checkPermissionLevel(20), departmentController.getDepartment);

// 创建部门
router.post('/', checkPermission('department:create'),checkPermissionLevel(40), departmentController.createDepartment);

// 更新部门
router.put('/update', checkPermission('department:update'),checkPermissionLevel(40), departmentController.updateDepartment);

// 删除部门
router.delete('/:id', checkPermission('department:delete'),checkPermissionLevel(40), departmentController.removeDepartment);

// 获取子部门
router.get('/:id/children', checkPermission('department:view:children'),checkPermissionLevel(20), departmentController.getChildrenDepartments);

// 获取所有子部门
router.get('/:id/children/all', checkPermission('department:view:children:all'),checkPermissionLevel(20), departmentController.getAllChildrenDepartments);

// 获取父部门
router.get('/:id/parents', checkPermission('department:view:parents'),checkPermissionLevel(20), departmentController.getParentDepartments);

// 获取部门用户数量
router.get('/:id/users/count', checkPermission('department:view:users'), checkPermissionLevel(20) , departmentController.getDepartmentUserCount);

// 检查部门是否可以删除
router.get('/:id/deletable', checkPermission('department:check:delete'), checkPermissionLevel(40) ,departmentController.checkDepartmentDeletable);

module.exports = router;
